//
//  ModifyPasswordViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/14.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "ModifyPasswordViewController.h"
#import "CEAccessoryTextField.h"

@interface ModifyPasswordViewController ()<UITextFieldDelegate>

/** 原密码 */
@property(nonatomic, strong) CEAccessoryTextField *oldpasswordTextField;
/** 新密码 */
@property(nonatomic, strong) CEAccessoryTextField *newpasswordTextField;
/** 确认密码 */
@property(nonatomic, strong) CEAccessoryTextField *confirmpasswordTextField;
/** 登录按钮 */
@property(nonatomic, strong) UIButton *sureButton;

@end

@implementation ModifyPasswordViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"确认密码";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupConstraints];

    
}

- (void)saveAction {
    
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [self resignAllTextFieldResponder];
}


#pragma mark - Constraints

- (void)setupConstraints {
    
    
    [self.oldpasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@35);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(@50);
    }];
    
    [self.newpasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.oldpasswordTextField.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.oldpasswordTextField);
        make.right.equalTo(self.oldpasswordTextField);
        make.height.mas_equalTo(@50);
    }];
    
    [self.confirmpasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.newpasswordTextField.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.oldpasswordTextField);
        make.right.equalTo(self.oldpasswordTextField);
        make.height.mas_equalTo(@50);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.confirmpasswordTextField.mas_bottom).offset(20);
        make.centerX.equalTo(self.view);
        make.left.mas_equalTo(@50);
        make.right.mas_equalTo(@-50);
        make.height.mas_equalTo(@50);
    }];
    
   
}

#pragma mark - Lazy Loading
- (CEAccessoryTextField *)oldpasswordTextField {
    
    if (!_oldpasswordTextField) {
        
        _oldpasswordTextField = [[CEAccessoryTextField alloc] init];
        _oldpasswordTextField.type = CEAccessoryTextFieldTypeLabel;
        _oldpasswordTextField.titleLabel.text = @"原密码";
        _oldpasswordTextField.delegate = self;
        
        [self.view addSubview:_oldpasswordTextField];
    }
    return _oldpasswordTextField;
}

- (CEAccessoryTextField *)newpasswordTextField {
    
    if (!_newpasswordTextField) {
        
        _newpasswordTextField = [[CEAccessoryTextField alloc] init];
        _newpasswordTextField.type = CEAccessoryTextFieldTypeLabel;
        _newpasswordTextField.titleLabel.text = @"新密码";
        _newpasswordTextField.delegate = self;
        
        [self.view addSubview:_newpasswordTextField];
    }
    return _newpasswordTextField;
}

- (CEAccessoryTextField *)confirmpasswordTextField {
    
    if (!_confirmpasswordTextField) {
        
        _confirmpasswordTextField = [[CEAccessoryTextField alloc] init];
        _confirmpasswordTextField.type = CEAccessoryTextFieldTypeLabel;
        _confirmpasswordTextField.titleLabel.text = @"确认密码";
        _confirmpasswordTextField.delegate = self;
        
        [self.view addSubview:_confirmpasswordTextField];
    }
    return _confirmpasswordTextField;
}


- (UIButton *)sureButton {
    
    if (!_sureButton) {
        
        _sureButton = [[UIButton alloc] init];
        _sureButton.backgroundColor = kColorTint;
        _sureButton.layer.cornerRadius = 5.0f;
        _sureButton.layer.masksToBounds = YES;
        
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [_sureButton setTitle:@"确定" forState:UIControlStateHighlighted];
        
        @weakify(self);
        [[_sureButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            
            
            NSString *oldPwd = _oldpasswordTextField.text;
            NSString *newPwd = _newpasswordTextField.text;
            NSString *confirmPwd = _confirmpasswordTextField.text;
            
            if (oldPwd.length == 0) {
                ShowMessage(@"请输入原密码");
                return;
            }
            
            if (newPwd.length == 0) {
                ShowMessage(@"请输入密码");
                return;
            }
            
            if (confirmPwd.length == 0) {
                ShowMessage(@"请输入确认密码");
                return;
            }
            
            if (![newPwd isEqualToString:confirmPwd]) {
                ShowMessage(@"新密码与确认密码必须一致");
                return;
            }
            
            NSDictionary *params = @{@"account":[UserManager sharedInstance].userModel.value.mobile,
                                     @"newPassword":newPwd,
                                     @"password":oldPwd.mj_JSONString};
            
            [MLAHttpManage postHttpType:MLAHttpRequestType_Post hostType:MLAHttpHostType_Api api:@"/api/v1/users/actions/resetPassword" params:params.mj_JSONString success:^(id  _Nonnull response, NSString * _Nonnull msg) {
                ShowMessage(@"修改成功");
                [self.navigationController popViewControllerAnimated:YES];
            } failure:^{
                ShowMessage(@"修改失败,请重新修改");
            }];

        }];
        
        [self.view addSubview:_sureButton];
    }
    return _sureButton;
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - UIControl

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignAllTextFieldResponder];
}

#pragma mark - Private

- (void)resignAllTextFieldResponder {
    
    [self.oldpasswordTextField resignFirstResponder];
    [self.newpasswordTextField resignFirstResponder];
    [self.confirmpasswordTextField resignFirstResponder];

}

@end
